Skip to content

Python: Add music player example#158

Merged
hillary-mutisya merged 3 commits intomainfrom
users/hillarym/music
Jan 3, 2024
Merged

Python: Add music player example#158
hillary-mutisya merged 3 commits intomainfrom
users/hillarym/music

Conversation

@hillary-mutisya
Copy link
Copy Markdown
Contributor

This change adds a music player example to the python typechat implementation. Unlike the typescript edition, this example does not rely on JsonProgram. Instead, the music player schema is exposed as a set of actions (and their corresponding parameter types). The model selects a sequence of actions to run. These are passed to a handler function that executes them in order.

The code is organized as follows:

  • The demo.py file is the console app entry point - this is similar to previous python examples.
  • The schema.py file defines the action types and their associated parameters.
  • The notebooks/music.pynb file has an interactive Jupyter notebook for the music scenario.
  • The client.py file has the logic for executing schmatized actions against the spotify API
  • For this example, we are using the popular Spotipy (https://spotipy.readthedocs.io/en/2.22.1/) package to interact with spotfiy's API. This package does not currently provide type hints on its method,, so we have added code in spotipyWrapper.py that wraps the methods that we use. This file also has stub implementations of the data contracts we use in the example. Spotipy2 (https://pypi.org/project/spotipy2/) is a promising effort that has type hints and async method definitions - it's currently missing key parts of Spotify automation that we use (e.g. controlling playback on a particular device). ''Once Spotipy2 is ready, we can retire the wrapper code.

Copy link
Copy Markdown
Member

@DanielRosenwasser DanielRosenwasser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks promising! I didn't see much else that jumped out at me. I do want to give it a try locally, and I think @steveluc might want to look things over.

Comment thread python/examples/music/client.py


async def handle_call(action: PlayerAction, context: ClientContext):
match action["actionName"]:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something we could do is use a class with a bunch of methods with hasattr. I've suggested we could do something similar with a plain object in the TypeScript implementation too.

For now it is probably fine as-is. I wouldn't rewrite everything unless you think you can do it quickly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been looking for a way to verify type signatures at runtime. As far as I can tell, the TypedDicts are just dicts at runtime so we cannot "assert x is T" or "cast x as T" to ensure the right properties are present, Let me know if you find a way to do this.

Copy link
Copy Markdown
Contributor

@steveluc steveluc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for getting this one ported! It looks good. I'd love to try running it locally. Let's merge and try it out.

@hillary-mutisya hillary-mutisya merged commit 9eb38eb into main Jan 3, 2024
Copy link
Copy Markdown
Contributor

@pcdeadeasy pcdeadeasy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@DanielRosenwasser DanielRosenwasser deleted the users/hillarym/music branch January 3, 2024 23:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants